Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
[Node.js]How to access files in different directory using readFile

Under directory main:

  • css(foler)
    • stylesheet.css
  • js(folder)
    • data.js
    • server.js
  • index.html

And inside server.js:


    //Cannot access index.html
    file.readFile('index.html', 'utf8', function(err, contents){
      //some code
    });

Specifically, how do we access stylesheet.css and index.html

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

For accessing folders on a top level directory you can use .. in your path.

Access index.html in server.js

//access top level directory using `..`
file.readFile('../index.html', 'utf8', function(err, contents){
  //some code
});

file.readFile('../css/stylesheet.css', 'utf8', function(err, contents){
  //some code
});

Ideally you would always import from absolute paths. That prevents breaking your app when you restructure it. I suggest to always start your app from the root directory of your project like node js/server.js .

When starting it from root the process.cwd() (current working directory.) variable will always point to the root dir of the project and file reads could be done using:

var { join } = require('path')
var htmlFile = join(process.cwd(), 'index.html')
var cssFile = join(process.cwd(), 'css', 'stylesheet.css')

// read html file
file.readFile(htmlFile, 'utf8', function(err, contents){
  //some code
});

// ...
about 4 years ago · Juan Pablo Isaza Relatório

0

when you start the server, a global variable called __dirname is set that holds the absolute path of the folder that server.js is inside. Now we can use that value to read files relative to server.js file. In the following code, I used path.join() to construct the path to the files that you want to access. The double dots (..) is the directory above the js folder.

const fs = require("fs");
const path = require("path");

// Get the absulote path of the files
const htmlFile = path.join(__dirname, "..", "index.html");
const cssFile = path.join(__dirname, "..", "css", "stylesheet.css");

// Read files
fs.readFile(htmlFile, "utf8", function (err, content) {
  console.log(content);
});

fs.readFile(cssFile, "utf8", function (err, content) {
  console.log(content);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Try add ".", so it will be :

fs.readFile("./../index.html", "utf8", (err, content){ return }) 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda